﻿using Apps.Common;
using Apps.Models;
using System.Linq;
using System.Collections.Generic;
using System;
using Apps.Models.PL;
using Apps.BLL.Core;
using Apps.Locale;
using Apps.IBLL.DI;
using Microsoft.Practices.Unity;
using Apps.IDAL.DI;
using Apps.Models.DI;
using System.Linq.Expressions;

namespace Apps.BLL.PL
{

    
    public partial class PL_RC_DETAILSBLL
    {
      

        public override  List<PL_RC_DETAILSModel> GetListByParentId(ref GridPager pager, string queryStr, object parentId)
        {
            using (DBContainer db = new DBContainer())
            {
                string parid = parentId.ToString();
                var whereLambda = PredicateBuilder.True<PL_RC_DETAILS>();
                if (!string.IsNullOrWhiteSpace(queryStr))
                {
                    whereLambda = whereLambda.And(a => (
                                        a.TRANSPORT_ID.Contains(queryStr)
                                       || a.SUPPLIER_ID.Contains(queryStr)
                                       || a.REMARKS.Contains(queryStr)
                                       || a.PLAN_ID.Contains(queryStr)
                                       || a.MINE_ID.Contains(queryStr)
                                       || a.MATERIEL_ID.Contains(queryStr)
                                       || a.END_TIME.Contains(queryStr)
                                       || a.DETAIL_ID.Contains(queryStr)
                                       || a.CONTRACT_DETAIL_ID.Contains(queryStr)
                                       || a.BEGIN_TIME.Contains(queryStr)
                                       || a.BATCH_CODE.Contains(queryStr)
                    ));
                }

                if (!string.IsNullOrEmpty(parid))
                {
                    whereLambda = whereLambda.And(p => p.PLAN_ID == parid);
                }
               

                IQueryable<PL_RC_DETAILS> queryData = db.PL_RC_DETAILS.Where(whereLambda.Compile()).AsQueryable();
                pager.totalRows = queryData.Count();
                //排序
                queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
                return CreateModelList(ref queryData); ;
            }
        }

       

        public virtual decimal GetBathCode(DateTime BathCodeDate)
        {
            string datetime = BathCodeDate.ToString("yyyy-MM-dd");
            return m_Rep.Context.Database.SqlQuery<Decimal>($"SELECT FUN_GET_BATCHCODE(to_date('{datetime}','yyyy-MM-dd')) FROM DUAL").FirstOrDefault();
        }
        public override List<PL_RC_DETAILSModel> CreateModelList(ref IQueryable<PL_RC_DETAILS> queryData)
        {

            List<PL_RC_DETAILSModel> modelList = (from r in queryData
                                                  select new PL_RC_DETAILSModel
                                                  {
                                                      DETAIL_ID = r.DETAIL_ID,
                                                      PLAN_ID = r.PLAN_ID,
                                                      BATCH_CODE = r.BATCH_CODE,
                                                      AMOUNT = r.AMOUNT,
                                                      QNET = r.QNET,
                                                      ST = r.ST,
                                                      PLAN_DATE = r.PLAN_DATE,
                                                      BEGIN_TIME = r.BEGIN_TIME,
                                                      END_TIME = r.END_TIME,
                                                      MATERIEL_ID = r.MATERIEL_ID,
                                                      SUPPLIER_ID = r.SUPPLIER_ID,
                                                      MINE_ID = r.MINE_ID,
                                                      TRANSPORT_ID = r.TRANSPORT_ID,
                                                      REMARKS = r.REMARKS,
                                                      CONTRACT_DETAIL_ID = r.CONTRACT_DETAIL_ID,
                                                      ARTIFICIAL_CODE=r.ARTIFICIAL_CODE,
                                                      MATERIELName=r.PUB_MATERIEL==null?string.Empty:r.PUB_MATERIEL.MATERIEL_NAME,
                                                      SUPPLIERName=r.PUB_SUPPLIER==null? string.Empty : r.PUB_SUPPLIER.SUPPLIER_NAME,
                                                      MINEName=r.PUB_MINE==null? string.Empty : r.PUB_MINE.MINE_NAME,
                                                      TRANSPORTName=r.PUB_TRANSPORT==null? string.Empty : r.PUB_TRANSPORT.TRANSPORT_NAME
                                                  }).ToList();
            return modelList;
        }

        public List<V_RC_WEIGHT_UP_MAIN> GetListWeightUpMain(ref GridPager pager, string queryStr)
        {
            using (DBContainer db = new DBContainer())
            {
                var whereLambda = PredicateBuilder.True<V_RC_WEIGHT_UP_MAIN>();
                if (!string.IsNullOrWhiteSpace(queryStr))
                {
                    whereLambda = whereLambda.And(a => ((a.MINE_NAME!=null&&a.MINE_NAME.Contains(queryStr))
                                       ||(a.MATERIEL_NAME!=null&& a.MATERIEL_NAME.Contains(queryStr))
                                       || (a.SUPPLIER_NAME!=null&&a.SUPPLIER_NAME.Contains(queryStr))
                                       || (a.TRANSPORT_NAME!=null&&a.TRANSPORT_NAME.Contains(queryStr))
                                       || a.BATCH_CODE.Contains(queryStr)
                                       || (a.UPLOAD_BATCHCODE!=null&&a.UPLOAD_BATCHCODE.Contains(queryStr))
                    ));
                }

                //IQueryable<V_RC_WEIGHT_UP_MAIN> queryData = db.V_RC_WEIGHT_UP_MAIN.Where(whereLambda.Compile()).GroupBy(p => new
                //{
                //    p.BATCH_CODE,
                //    p.DETAIL_ID,
                //    p.UPLOAD_BATCHCODE,
                //    p.SUPPLIER_NAME,
                //    p.MATERIEL_NAME,
                //    p.MINE_NAME,
                //    p.UPLOAD_STATUS,
                //    p.CONFIRM_TIME,
                //    p.CONFIRM_USER,
                //    p.UPLOAD_TIME,
                //    p.EXTERNAL_CODE

                //}).Select(q => new V_RC_WEIGHT_UP_MAIN {
                //    BATCH_CODE=q.Key.BATCH_CODE,
                //    DETAIL_ID = q.Key.DETAIL_ID,
                //    UPLOAD_BATCHCODE = q.Key.UPLOAD_BATCHCODE,
                //    SUPPLIER_NAME = q.Key.SUPPLIER_NAME,
                //    MATERIEL_NAME = q.Key.MATERIEL_NAME,
                //    MINE_NAME = q.Key.MINE_NAME,
                //    UPLOAD_STATUS = q.Key.UPLOAD_STATUS,
                //    CARCODE = q.Sum(i => i.CARCODE),
                //    NET = q.Sum(i => i.NET),
                //    BALANCE = q.Sum(i => i.BALANCE),
                //    BILL_WEIGHT=q.Sum(i=>i.BILL_WEIGHT),
                //    CONFIRM_TIME=q.Key.CONFIRM_TIME,
                //    CONFIRM_USER=q.Key.CONFIRM_USER,
                //    UPLOAD_TIME=q.Key.UPLOAD_TIME,
                //    EXTERNAL_CODE=q.Key.EXTERNAL_CODE
                //}).AsQueryable();
                IQueryable<V_RC_WEIGHT_UP_MAIN> queryData = db.V_RC_WEIGHT_UP_MAIN.Where(whereLambda.Compile()).AsQueryable();
                pager.totalRows = queryData.Count();
                //排序
                queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
                return queryData.ToList();

            }
        }

        public List<V_LMS_UP> GetLmsUpList(ref GridPager pager, string queryStr)
        {
            using (DBContainer db = new DBContainer())
            {
                var whereLambda = PredicateBuilder.True<V_LMS_UP>();
                if (!string.IsNullOrWhiteSpace(queryStr))
                {
                    whereLambda = whereLambda.And(a => ((a.MINE_NAME != null && a.MINE_NAME.Contains(queryStr))
                                       || (a.MATERIEL_NAME != null && a.MATERIEL_NAME.Contains(queryStr))
                                       || (a.SUPPLIER_NAME != null && a.SUPPLIER_NAME.Contains(queryStr))
                                       || (a.TRANSPORT_NAME != null && a.TRANSPORT_NAME.Contains(queryStr))
                                       || a.BATCH_CODE.Contains(queryStr)
                                       || a.TEST_CODE.Contains(queryStr)
                    ));
                }

                IQueryable<V_LMS_UP> queryData= db.V_LMS_UP.Where(whereLambda.Compile()).AsQueryable();
                
                pager.totalRows = queryData.Count();
                //排序
                queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
                List<V_LMS_UP> queryData2= (from q in queryData.ToList()
                                                 join u in db.SysUser on q.CONFIRM_USER equals u.Id into result1
                                                 from x in result1.DefaultIfEmpty ()
                                                 select new V_LMS_UP
                                                 {
                                                     AAD = q.AAD,
                                                     AAR = q.AAR,
                                                     AD = q.AD,
                                                     BATCH_CODE = q.BATCH_CODE,
                                                     CONFIRM_TIME = q.CONFIRM_TIME,
                                                     CRC = q.CRC,
                                                     DETAIL_ID = q.DETAIL_ID,
                                                     EXTERNAL_CODE = q.EXTERNAL_CODE,
                                                     FCAD = q.FCAD,
                                                     HAD = q.HAD,
                                                     MAD = q.MAD,
                                                     CONFIRM_USER=(x==null?"":x.TrueName),
                                                     MAKE_TYPE = q.MAKE_TYPE,
                                                     MATERIEL_ID = q.MATERIEL_ID,
                                                     MATERIEL_NAME = q.MATERIEL_NAME,
                                                     MINE_ID = q.MINE_ID,
                                                     MINE_NAME = q.MINE_NAME,
                                                     MT = q.MT,
                                                     QBAD = q.QBAD,
                                                     QGRAD = q.QGRAD,
                                                     QGRD = q.QGRD,
                                                     QNETAR_KCAL = q.QNETAR_KCAL,
                                                     QNETAR_MJ = q.QNETAR_MJ,
                                                     STAD = q.STAD,
                                                     STAR = q.STAR,
                                                     STD = q.STD,
                                                     STDAF = q.STDAF,
                                                     SUPPLIER_ID = q.SUPPLIER_ID,
                                                     SUPPLIER_NAME = q.SUPPLIER_NAME,
                                                     TEST_CODE = q.TEST_CODE,
                                                     TRANSPORT_ID = q.TRANSPORT_ID,
                                                     TRANSPORT_NAME = q.TRANSPORT_NAME,
                                                     UPLOAD_STATUS = q.UPLOAD_STATUS,
                                                     UPLOAD_TIME = q.UPLOAD_TIME,
                                                     VAD = q.VAD,
                                                     VAR = q.VAR,
                                                     VD = q.VD,
                                                     VDAF = q.VDAF
                                                 }).ToList();
                return queryData2;

            }
        }



        public V_LMS_UP GetViewEntityByLmsUp(string DetailID)
        {
            using (DBContainer db = new DBContainer())
            {
                V_LMS_UP entity = db.V_LMS_UP.Find(DetailID);
                return entity;

            }
        }


        public List<V_RC_WEIGHT_UP_MAIN> GetViewEntity(string DetailID)
        {
            using (DBContainer db = new DBContainer())
            {
                List<V_RC_WEIGHT_UP_MAIN> entitylist = db.V_RC_WEIGHT_UP_MAIN.Where(p=>p.DETAIL_ID==DetailID).ToList();
                return entitylist;

            }
        }


        public List<V_RC_WEIGHT_UP_DETAIL> GetListWeightUpDetail(ref GridPager pager, string querStr)
        {
            using (DBContainer db = new DBContainer())
            {
                var whereLambda = PredicateBuilder.True<V_RC_WEIGHT_UP_DETAIL>();
                if (!string.IsNullOrWhiteSpace(querStr))
                {
                    whereLambda = whereLambda.And(p => p.UPLOAD_BATCHCODE==querStr);
                }
                IQueryable<V_RC_WEIGHT_UP_DETAIL> queryData = db.V_RC_WEIGHT_UP_DETAIL.Where(whereLambda.Compile()).AsQueryable();
                pager.totalRows = queryData.Count();
                //排序
                queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
                return queryData.ToList();

            }
        }

        public List<V_RC_IN> GetListCadIN(ref GridPager pager,string querStr, string sDate, string eDate)
        {
            using (DBContainer db = new DBContainer())
            {
                var whereLambda = PredicateBuilder.True<V_RC_IN>();
                whereLambda = whereLambda.And(p => p.IS_COAL != 0);
                if (!string.IsNullOrWhiteSpace(querStr))
                {
                    whereLambda = whereLambda.And(p=> (p.LPR!=null&&p.LPR.Contains(querStr))
                    || (p.MATCH_USER_NAME!=null&&p.MATCH_USER_NAME.Contains(querStr))
                    || (p.MATERIEL_NAME!=null&&p.MATERIEL_NAME.Contains(querStr))
                    || (p.MINE_NAME!=null&&p.MINE_NAME.Contains(querStr))
                    || (p.NODE_NAME!=null&&p.NODE_NAME.Contains(querStr))
                    || (p.SUPPLIER_NAME!=null&&p.SUPPLIER_NAME.Contains(querStr))
                    || (p.TRANSPORT_NAME!=null&&p.TRANSPORT_NAME.Contains(querStr))
                    || (p.BATCH_CODE!=null&&p.BATCH_CODE.Contains(querStr))
                    );
                }

                if (string.IsNullOrWhiteSpace(sDate))
                {
                    sDate = DateTime.Now.AddYears(-30).ToString("yyyy-MM-dd");
                }
                if (string.IsNullOrWhiteSpace(eDate))
                {
                    eDate = DateTime.Now.AddYears(30).ToString("yyyy-MM-dd");
                }

                DateTime dt1 = Convert.ToDateTime(sDate + " 00:00:00");
                DateTime dt2 = Convert.ToDateTime(eDate + " 23:59:59");
                whereLambda = whereLambda.And(p => p.MATCH_TIME >= dt1 && p.MATCH_TIME <= dt2);
                IQueryable<V_RC_IN> queryData = db.V_RC_IN.Where(whereLambda.Compile()).AsQueryable();
                pager.totalRows = queryData.Count();
                //排序
                queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);

                return queryData.ToList();
            }
        }

        public List<V_RC_OUT> GetListOut(ref GridPager pager, string querStr, string sDate, string eDate)
        {
            using (DBContainer db = new DBContainer())
            {
                var whereLambda = PredicateBuilder.True<V_RC_OUT>();
                whereLambda = whereLambda.And(p => p.IS_COAL==true);
                if (!string.IsNullOrWhiteSpace(querStr))
                {
                    whereLambda = whereLambda.And(p => (p.LPR!=null &&p.LPR.Contains(querStr))
                    || (p.MATCH_USER_NAME!=null &&p.MATCH_USER_NAME.Contains(querStr))
                    || (p.MATERIEL_NAME!=null&&p.MATERIEL_NAME.Contains(querStr))
                    || (p.MINE_NAME!=null&&p.MINE_NAME.Contains(querStr))
                    || (p.NODE_NAME!=null&&p.NODE_NAME.Contains(querStr))
                    || (p.SUPPLIER_NAME!=null&&p.SUPPLIER_NAME.Contains(querStr))
                    || (p.TRANSPORT_NAME!=null&&p.TRANSPORT_NAME.Contains(querStr))
                    || (p.BATCH_CODE!=null&&p.BATCH_CODE.Contains(querStr))
                    );
                }

                if (string.IsNullOrWhiteSpace(sDate))
                {
                    sDate = DateTime.Now.AddYears(-30).ToString("yyyy-MM-dd");
                }
                if (string.IsNullOrWhiteSpace(eDate))
                {
                    eDate = DateTime.Now.AddYears(30).ToString("yyyy-MM-dd");
                }
                DateTime dt1 = Convert.ToDateTime(sDate + " 00:00:00");
                DateTime dt2 = Convert.ToDateTime(eDate + " 23:59:59");
                whereLambda = whereLambda.And(p => p.MATCH_TIME >= dt1 && p.MATCH_TIME <= dt2);

      

                IQueryable<V_RC_OUT> queryData = db.V_RC_OUT.Where(whereLambda.Compile()).AsQueryable();
                pager.totalRows = queryData.Count();
                //排序
                queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
                return queryData.ToList();
            }
        }
        public override PL_RC_DETAILSModel GetById(object id)
        {
            if (IsExists(id))
            {

                PL_RC_DETAILS entity = m_Rep.GetById(id);
                PL_RC_DETAILSModel model = new PL_RC_DETAILSModel();
                model.DETAIL_ID = entity.DETAIL_ID;
                model.PLAN_ID = entity.PLAN_ID;
                model.BATCH_CODE = entity.BATCH_CODE;
                model.AMOUNT = entity.AMOUNT;
                model.QNET = entity.QNET;
                model.ST = entity.ST;
                model.PLAN_DATE = entity.PLAN_DATE;
                model.BEGIN_TIME = entity.BEGIN_TIME;
                model.END_TIME = entity.END_TIME;
                model.MATERIEL_ID = entity.MATERIEL_ID;
                model.SUPPLIER_ID = entity.SUPPLIER_ID;
                model.MINE_ID = entity.MINE_ID;
                model.TRANSPORT_ID = entity.TRANSPORT_ID;
                model.REMARKS = entity.REMARKS;
                model.CONTRACT_DETAIL_ID = entity.CONTRACT_DETAIL_ID;
                return model;
            }
            else
            {
                return null;
            }
        }

       

        public List<PL_RC_DETAILS> QueryUnStockInList()
        {
            return m_Rep.QueryUnStockInList().ToList();
        }

        public List<V_PL_RC_DETAILS> GetDetailList(Expression<Func<V_PL_RC_DETAILS, bool>> whereLambda)
        {
            using (DBContainer db = new DBContainer())
            {
                IQueryable<V_PL_RC_DETAILS> queryData = db.V_PL_RC_DETAILS.Where(whereLambda.Compile()).AsQueryable();
                //排序
                return queryData.ToList();

            }
        }
    }
}

